home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / yahoo / YahooSocket.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  6KB  |  199 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from __future__ import with_statement
  5. DEFAULT_YMSG_VERSION = 15
  6. from yahoo.yahooutil import *
  7. from util import to_storage, unpack_named, dictargcall, traceguard
  8. import common
  9. from pprint import pformat
  10. import sys
  11. import traceback
  12. from logging import getLogger
  13. log = getLogger('yahoo')
  14. loginlog = getLogger('yahoo.login')
  15. packets_log = getLogger('yahoo.packets')
  16.  
  17. class YahooSocket(common.socket):
  18.     
  19.     def __init__(self, yahoo, server):
  20.         common.socket.__init__(self)
  21.         self.yahoo = yahoo
  22.         self.session_id = 0
  23.         self.active_gens = { }
  24.         self.set_terminator(header_size)
  25.         self.getting_header = True
  26.         self.data = []
  27.         self.byte_count = 0
  28.         self.server = server
  29.  
  30.     
  31.     def __str__(self):
  32.         return 'YahooSocket(%s:%d, %d bytes in)' % (self.server[0], self.server[1], self.byte_count)
  33.  
  34.     
  35.     def make_ypacket(self, service, status, version = DEFAULT_YMSG_VERSION, data = ''):
  36.         if not isinstance(service, (int, long)):
  37.             raise TypeError('service is', service, 'but should be an int!')
  38.         
  39.         if not isinstance(status, (int, long)):
  40.             raise TypeError('status is', status, 'but should be an int!')
  41.         
  42.         return pack(header_pack, 'YMSG', version, 0, len(data), service, status, self.session_id) + data
  43.  
  44.     
  45.     def ysend(self, service, status, version = DEFAULT_YMSG_VERSION, data = ''):
  46.         packet = self.make_ypacket(service, status, version, data)
  47.         self.push(packet)
  48.  
  49.     
  50.     def handle_packet(self, header, data):
  51.         self.session_id = header.session_id
  52.         if (header.service, header.status) in self.active_gens:
  53.             gen = self.active_gens.pop((header.service, header.status))
  54.             self.async_proc(gen, header, data)
  55.             return None
  56.         
  57.         sv_str = services.get(header.service, '')
  58.         st_str = statuses.get(header.status, '')
  59.         target = self.yahoo
  60.         fnname = '%s_%s' % (sv_str, st_str)
  61.         traceguard.__enter__()
  62.         
  63.         try:
  64.             packet_str = format_packet(data, sensitive = not getattr(sys, 'DEV', True))
  65.             packets_log.debug('~~> %s\n%s', fnname, packet_str)
  66.         finally:
  67.             pass
  68.  
  69.         raw_fn = '%s_raw' % fnname
  70.         if hasattr(target, raw_fn):
  71.             
  72.             try:
  73.                 return getattr(target, raw_fn)(from_ydict_iter(data))
  74.             except Exception:
  75.                 traceguard
  76.                 traceguard
  77.                 traceback.print_exc()
  78.             except:
  79.                 traceguard
  80.             finally:
  81.                 return None
  82.  
  83.         
  84.         if sv_str and st_str:
  85.             fn = '%s_%s' % (sv_str, st_str)
  86.             if hasattr(target, fn):
  87.                 func = getattr(target, fn)
  88.                 
  89.                 try:
  90.                     return dictargcall(func, from_ydict(data), ykeys)
  91.                 except Exception:
  92.                     traceback.print_exc()
  93.                     print >>sys.stderr, '  File "%s", line %d, in %s' % (func.func_code.co_filename, func.func_code.co_firstlineno, func.func_name)
  94.                     return None
  95.                 except:
  96.                     None<EXCEPTION MATCH>Exception
  97.                 
  98.  
  99.             None<EXCEPTION MATCH>Exception
  100.         
  101.         unhandled_text = None([
  102.             None,
  103.             ''.join,
  104.             'unhandled packet: ' if sv_str else str(header.service),
  105.             '_' if st_str else str(header.status)])
  106.         log.warning('%s:::%r', unhandled_text, data)
  107.  
  108.     
  109.     def async_proc(self, gen, header = None, data = None):
  110.         
  111.         try:
  112.             args = None if not header else (header, data)
  113.             (cmd, wait_on, packet) = gen.send(args)
  114.             while cmd == 'send':
  115.                 self.push(packet)
  116.                 (cmd, wait_on, packet) = gen.next()
  117.             if cmd == 'wait':
  118.                 self.active_gens[wait_on] = gen
  119.         except StopIteration:
  120.             pass
  121.         except YahooLoginError:
  122.             self.yahoo.set_disconnected(self.yahoo.Reasons.BAD_PASSWORD)
  123.  
  124.  
  125.     
  126.     def gsend(self, sv, st, data = '', **kwargs):
  127.         if 'v' not in kwargs:
  128.             v = DEFAULT_YMSG_VERSION
  129.         else:
  130.             v = kwargs['v']
  131.         if isinstance(sv, str):
  132.             sv = services[sv]
  133.         
  134.         if isinstance(st, str):
  135.             st = statuses[st]
  136.         
  137.         if isinstance(data, (dict, list)):
  138.             data = to_ydict(data)
  139.         
  140.         packet = self.make_ypacket(sv, st, v, data)
  141.         return ('send', None, packet)
  142.  
  143.     
  144.     def gwait(self, sv, st, err_fn):
  145.         sv = services[sv]
  146.         st = statuses[st]
  147.         return ('wait', (sv, st), err_fn)
  148.  
  149.     
  150.     def __repr__(self):
  151.         return '<%s(%s:%d) - sId: %s, bytes in: %d>' % (self.__class__.__name__, self.server[0], self.server[1], self.session_id, self.byte_count)
  152.  
  153.     
  154.     def handle_connect(self):
  155.         raise NotImplementedError
  156.  
  157.     
  158.     def handle_close(self):
  159.         raise NotImplementedError
  160.  
  161.     
  162.     def collect_incoming_data(self, data):
  163.         self.data.append(data)
  164.         self.byte_count += len(data)
  165.  
  166.     
  167.     def handle_error(self, *a, **k):
  168.         traceback.print_exc()
  169.         raise NotImplementedError
  170.  
  171.     
  172.     def handle_expt(self):
  173.         raise NotImplementedError
  174.  
  175.     
  176.     def found_terminator(self):
  177.         datastr = ''.join(self.data)
  178.         if self.getting_header:
  179.             self.getting_header = False
  180.             self.header = to_storage(unpack_named(*header_desc + tuple([
  181.                 datastr])))
  182.             if self.header.ymsg != 'YMSG' or self.header.size < 0:
  183.                 return log.warning('invalid packet')
  184.             
  185.             if self.header.size > 0:
  186.                 self.set_terminator(self.header.size)
  187.             else:
  188.                 self.getting_header = True
  189.                 self.set_terminator(header_size)
  190.                 self.handle_packet(self.header, datastr)
  191.                 self.data = []
  192.         else:
  193.             self.getting_header = True
  194.             self.set_terminator(header_size)
  195.             self.handle_packet(self.header, datastr[header_size:])
  196.             self.data = []
  197.  
  198.  
  199.